﻿using HMIControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CoreTest
{
    /// <summary>
    /// LoRaHMI.xaml 的交互逻辑
    /// </summary>
    public partial class LoRaHMI : UserControl
    {
        public LoRaHMI()
        {
            InitializeComponent();
            mt1.Running = true;
        }
        List<TagNodeHandle> _valueChangedList;
        Timer t1;
        private void HMI_Loaded(object sender, RoutedEventArgs e)
        {
            lock (this)
            {
                _valueChangedList = cvs1.BindingToServer(App.Server);
            }
            ben1.CommandCb = new Action<ValueSelectSwitch, string>(LoRaDO1Command_Sub);
            ben2.CommandCb = new Action<ValueSelectSwitch, string>(LoRaDO2Command_Sub);
            t1 = new Timer(new TimerCallback(t1_cb), null, 1000, 1000);


        }
        void t1_cb(object st)
        {
            this.Dispatcher.Invoke(()=> {

                int st1 = App.Server["1pRunStatus"].Read().Int16;
                mt1.Running = st1 == 3 || st1 == 4;
                int st2 = App.Server["2pRunStatus"].Read().Int16;
                mt2.Running = st2 == 3 || st2 == 4;

                int g1 = App.Server["LoRaDO1"].Read().Int16;
                guan1.Working = g1 == 1;
                int g2 = App.Server["LoRaDO2"].Read().Int16;
                guan2.Working = g2 == 1;

            });
    
        }


        private void LoRaDO1Command_Sub(ValueSelectSwitch Switch, string tag)
        {

            if (Switch.IsChecked == true)
            {
                App.Server[tag].Write(Switch.RightValue);
            }
            else
            {
                App.Server[tag].Write(Switch.LeftValue);
            }
        }
        private void LoRaDO2Command_Sub(ValueSelectSwitch Switch, string tag)
        {
            if (Switch.IsChecked == true)
            {
                App.Server[tag].Write(Switch.RightValue);
            }
            else
            {
                App.Server[tag].Write(Switch.LeftValue);
            }
        }

        private void HMI_Unloaded(object sender, RoutedEventArgs e)
        {
            lock (this)
            {
                App.Server.RemoveHandles(_valueChangedList);
            }
        }

        private void mt12_run_Click(object sender, RoutedEventArgs e)
        {
            var obj = App.Server["LoRaDO2"];
            obj.Write(1);
            obj.Write(0);
        }


    }
}
